<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
	}
	
	function index(){
		$this->load->helper('captcha');
		
		if($this->input->post('act') == 'login'){
			//$captcha		=	$_SESSION['captcha']; 
			
			$data = array(
				'captcha'  => trim($_POST['captcha']),
				'username' => $_POST['username'],
				'password' => $_POST['password'],
				'smscode' => $_POST['smscode'],
			);
			
			$ret = array('msg'=>'数据错误','url'=>'');
			
			if ($data['captcha'] =="" ){
				$ret['msg']='验证码不能为空';
				echo json_encode($ret);exit;
			}elseif ($data['captcha']!=$_SESSION['captcha']){
				$ret['msg']='验证码不正确';
				echo json_encode($ret);exit;
			}elseif ($data['username']==""){
				$ret['msg']='账号不能为空';
				echo json_encode($ret);exit;
			}elseif ($_POST['password']==""){
				$ret['msg']='密码不能为空';
				echo json_encode($ret);exit;
			}
			
//			elseif ($_POST['smscode']==""){
//				$ret['msg']='短信码不能为空';
//				echo json_encode($ret);exit;
//			}

			else{
				$this->load->model('systemadmin_user_model');
				$this->load->model('systemadmin_model');
				$user = $this->systemadmin_user_model->check_login($this->input->post('username'),$this->input->post('password'));
				$user['user_id'] = $user['id'];
				$user['header_list'] = $this->get_admin_list($user['type_id'],'header');//第一级管理权限目录
			//根据第一级目录获取二三级权限
				foreach($user['header_list'] as $k =>$v){
					$user[$k.'_list'] = $this->get_admin_list($user['type_id'],$k);
				}
				
				if(isset($user['user_id'])){
					if($user['status'] == 1){
						$user['login_curr_time'] = time();
						 $user['last_login_time'] = $user['login_time'];
						 $user['last_login_ip'] = $user['login_ip'];
						 
						$systype_info = $this->systemadmin_user_model->get_user_adminlist($user['type_id']);
						
						
						$user['type_name'] = $systype_info['name'];
						$this->session->set_userdata('admin_user',$user);
						$admin_user = $this->session->userdata('admin_user');
						$ret['msg']='';
						$last_url = trim($this->input->post('last_url'));
						
						if( $last_url == ''){
							reset($user['header_list']);
							$new_array = current($user['header_list']);
							$ret['url']=$new_array['link'];
						}else{
							$ret['url']=$last_url;
						}
						
						$usertime['login_time'] = time();
						$usertime['login_ip'] = $this->js->get_ip();
						$usertime['last_login_time'] = $user['login_time'];
						$usertime['last_login_ip'] = $user['login_ip'];
						$this->systemadmin_model->update($user['id'],$usertime);
						echo json_encode($ret);
						exit;
					}else{
						$ret['msg']='此账号已经关闭';
						echo json_encode($ret);
						exit;
					}
					 
				}else{
					$ret['msg']='账号或密码或验证码错误';
					echo json_encode($ret);
					exit;
				}
			}
		}else{
			$data['last_url']		    = trim($this->input->get('last_url'));
			$data['title'] 				= "后台登陆";
			$this->load->view('/ychadmin/login', $data);
		}
	}
	
	function sendsms(){
		$username = $this->input->post('username');
		$this->load->model('User_model', 'user');
		$user_obj = $this->user->get_one(array('username'=>$username),'user_id,phone');
		if($user_obj){
			$this->load->library('phone');
			$smscode = $this->phone->randstr();
			$msg = "后台登录手机验证码为：".$smscode;
			$res = $this->phone->send($user_obj['phone'],$msg);
			$this->user->update(array('smscode'=>$smscode, 'smstime'=>time()), array('user_id'=>$user_obj['user_id']));
			$ret['msg']='验证码发送成功,24小时内有效';
			echo json_encode($ret);exit;
		}
		$ret['msg']='用户名不存在';
		echo json_encode($ret);exit;
	}
	
	
	function get_admin_list($typeid,$show_where){
		$this->config->load('admin_type',TRUE);
		$admintype = $this->config->item('admintype', 'admin_type');
		$this->load->model('systemadmin_user_model');
		$admin_list = $this->systemadmin_user_model->get_user_adminlist($typeid);
		
	    $type_arr = explode(',',$admin_list['module']);
		$global = $borrow = $account = $user = $site = $shop = $system = $header = array();
		
		foreach ($admintype as $k=>$v){
			foreach($v['list'] as $lk => $lv){
				foreach($lv['sonlist'] as $sk =>$sv){
					if(array_key_exists(array_search($sk,$type_arr),$type_arr)){
						
						if($k=="global"){
								$global[$lv['sonname']][$sk][] = $sv['linkname'];
								$global[$lv['sonname']][$sk][] = $sv['link'];

						}
						
						if($k=="borrow"){
								$borrow[$lv['sonname']][$sk][] = $sv['linkname'];
								$borrow[$lv['sonname']][$sk][] = $sv['link'];

						}
						if($k=="account"){
								$account[$lv['sonname']][$sk][] = $sv['linkname'];
								$account[$lv['sonname']][$sk][] = $sv['link'];
						}
						if($k=="user"){
								$user[$lv['sonname']][$sk][] = $sv['linkname'];
								$user[$lv['sonname']][$sk][] = $sv['link'];

						}
						if($k=="shop"){
								$shop[$lv['sonname']][$sk][] = $sv['linkname'];
								$shop[$lv['sonname']][$sk][] = $sv['link'];
						}
						if($k=="site"){
								$site[$lv['sonname']][$sk][] = $sv['linkname'];
								$site[$lv['sonname']][$sk][] = $sv['link'];

						}
						if($k=="system"){
								$system[$lv['sonname']][$sk][] = $sv['linkname'];
								$system[$lv['sonname']][$sk][] = $sv['link'];
						}
					}
				}
			}
		}

		$header_type = $this->config->item('type', 'admin_type');
		foreach($header_type as $hk => $hv){
			
			if(array_key_exists(array_search($hk,$type_arr),$type_arr)){
				$header[$hv['sub']][link]=$hv['link'];
				$header[$hv['sub']][name]=$hv['grandpa'];
			}
		}
		switch($show_where){
			case 'global':
				return $global;
			break;
			
			case 'borrow':
				return $borrow;
			break;
			case 'account':
				return $account;
				break;
			case 'user':
				return $user;
			break;
			case 'shop':
				return $shop;
				break;
			case 'site':
				return $site;
			break;
			case 'system':
				return $system;
				break;
			case 'header':
				return $header;
		}
	}
	
	
}
